{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5fd79177",
   "metadata": {},
   "source": [
    "# 元组(tuple)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6253353",
   "metadata": {},
   "source": [
    "1.他是和list几乎完全一样的序列数据  \n",
    "2.它与list不同的地方是，不可变(因为不可变，所以几乎没有方法  \n",
    "3.元组的形式：（1，2，3，4，5）对比 元组（）列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "36588674",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('10001', '10002', '10003', '10001') <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "stu_id = \"10001\",\"10002\",\"10003\",\"10001\"\n",
    "print(stu_id,type(stu_id))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b905f22c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('10001', '10002', '10003', '10001')"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_id "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c57b9083",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5b111543",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2021, tm_mon=11, tm_mday=14, tm_hour=23, tm_min=38, tm_sec=40, tm_wday=6, tm_yday=318, tm_isdst=0)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "time.localtime()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86ad2aeb",
   "metadata": {},
   "source": [
    "# 字典（dict）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8dcbc301",
   "metadata": {},
   "source": [
    "1.创建字典的三种常见的方法  \n",
    "2.直接按照字典的格式创建：{key1:valuel,key2:value2,...} 2.dict()函数来创建字典：dict(key1=valuel,key2=value2,...)  \n",
    "3.字典数据增加的方式：新建空字典 dict_name = {},dict_name[key1]=value1..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0c44b697",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud_10001': 'Alex', 'stud_10002': 'Jack', 'stud_10003': 'Marry'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.直接按照字典的格式创建：{key1:valuel,key2:value2,...}\n",
    "stud_info = {\n",
    "    \"stud_10001\":\"Alex\",\n",
    "    \"stud_10002\":\"Jack\",\n",
    "    \"stud_10003\":\"Marry\"\n",
    "}\n",
    "stud_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "73bde3d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud_10001': 'Alex', 'stud_10002': 'Jack', 'stud_10003': 'Marry'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#2.dict()函数来创建字典：dict(key1=valuel,key2=value2,...)\n",
    "stud_info_02 = dict(\n",
    "    stud_10001 = \"Alex\",\n",
    "    stud_10002 = \"Jack\",\n",
    "    stud_10003 = \"Marry\",\n",
    "\n",
    ")\n",
    "stud_info_02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "69ffd323",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud_10001': 'Alex', 'stud_10002': 'Jack', 'stud_10003': 'Marry'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info_03 = {}\n",
    "stud_info_03[\"stud_10001\"] = \"Alex\"\n",
    "stud_info_03[\"stud_10002\"] = \"Jack\"\n",
    "stud_info_03[\"stud_10003\"] = \"Marry\"\n",
    "stud_info_03"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "fae89eb6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud_10001': 'Alex', 'stud_10002': 'Jack', 'stud_10003': 'Marry'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info_03"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b1fa0514",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['stud_10001', 'stud_10002', 'stud_10003'])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info_03.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "241826ba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values(['Alex', 'Jack', 'Marry'])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info_03.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fffed189",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "0bac6412",
   "metadata": {},
   "source": [
    "# 购物车"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fdb53131",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 购物车程序(商品名称，价格，编号)\n",
    "\n",
    "goods_list = [\n",
    "    [\"python书\",55],   #我们认为每一个商品的内容是一个多个值的值，可选用list\n",
    "    [\"iphone\",8000],\n",
    "    [\"vivo\",4000]\n",
    "]\n",
    "# 用户购物信息的数据存放\n",
    "shopping_list = [] # 用户列表初始为空，增加进来可考虑用append()方法\n",
    "# 用户输入余额的信息\n",
    "Alipay = int(input(\"请输入支付宝余额:\"))\n",
    "# 打印商品信息\n",
    "# info = '''----------goods list--------\n",
    "# 商品编号：%s,名称：%s,价格：%s 元\n",
    "# 商品编号：%s,名称：%s,价格：%s 元\n",
    "# 商品编号：%s,名称：%s,价格：%s 元\n",
    "# '''%(0,goods_list[0][0],goods_list[0][1],\n",
    "#      1,goods_list[1][0],goods_list[1][1],\n",
    "#      2,goods_list[2][0],goods_list[2][1],\n",
    "#     )\n",
    "# print(info)\n",
    "\n",
    "#list 的长度？len()\n",
    "#执行循环\n",
    "while True:\n",
    "    print('------商品清单 欢迎选购------')\n",
    "    #打印商品信息，打印商品的次数是由商品列表的长度决定的（len(goods_list))\n",
    "    for i in range(len(goods_list)):\n",
    "        print(\"商品编号：%s,名称：%s,价格：%s 元\" % (i,goods_list[i][0],goods_list[i][1]))  # %s占位符\n",
    "\n",
    "#2、让用户输入商品编号进行商品的购买\n",
    "\n",
    "     # 用户输入商品编号，实现商品的查询\n",
    "    choice = input(\"请输入商品的编号：\") #  *input 输入的内容默认是字符串str 例如输入2，则显示\"2\"\n",
    "    if choice.isdigit():     #isdigit 判断字符串里面的内容是否是数值\n",
    "        choice = int(choice) #如果是数值，把它变成真正的int\n",
    "        #判断编号是否在我们的范围内\n",
    "        if choice > len(goods_list):\n",
    "            print(\"你输入的商品不存在，请再次输入\")\n",
    "        else:\n",
    "        #检查用户余额 > 商品价格\n",
    "           if Alipay > goods_list[choice][1]:\n",
    "            # 买得起  1、扣钱，2、把商品加入到用户购物列表\n",
    "              Alipay = Alipay - goods_list[choice][1] #用户余额发生变化，要扣除\n",
    "              shopping_list.append(goods_list[choice])\n",
    "              print(\"商品[%s]已加入您的购物车，您的[余额]还有%s 元\"%(goods_list[choice][0],Alipay))\n",
    "           else:\n",
    "                 print(\"您的余额已不足，换个方式吧！\")\n",
    "                    \n",
    "    elif choice==\"q\":\n",
    "        # 输入q则说明用户要退出购买\n",
    "         #打印用户余额和用户的商品列表信息\n",
    "        print(\"用户余额 ：%s,购物车：%s\"%(Alipay,shopping_list))\n",
    "        break\n",
    "    else:\n",
    "         print(\"您输入的商品不存在，请输入正确的商品编号\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f4b4e63",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
